﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using TeamFoundation.EventBus.Client.Events;

namespace TeamFoundation.EventBus.Client
{
    /// <summary>
    /// Represents event information about a TFS event.
    /// </summary>
    public class TfsKnownEventArgs<T> : EventArgs
        where T : class
    {
        private static readonly XNamespace _rootNamespace = Utilities.EventNamespace;

        /// <summary>
        /// Gets the event data for the event.
        /// </summary>
        public XElement EventData
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the event.
        /// </summary>
        public T Event
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the identity data for the event.
        /// </summary>
        public XElement IdentityData
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the TFS identity.
        /// </summary>
        public TFSIdentity Identity
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the subscription ID for the event.
        /// </summary>
        public int SubscriptionId
        {
            get;
            private set;
        }

        private object _translated;
        /// <summary>
        /// The the translated object.
        /// </summary>
        public object Translate(Microsoft.TeamFoundation.Client.TeamFoundationServer tfs)
        {
            if (_translated == null)
            {
                ITfsTranslatable translate = Event as ITfsTranslatable;
                if (translate != null)
                {
                    _translated = translate.Translate(tfs);
                }
            }
            return _translated;
        }

        /// <summary>
        /// The the translated object.
        /// </summary>
        /// <typeparam name="T">The expected type of object.</typeparam>
        public T Translate<T>(Microsoft.TeamFoundation.Client.TeamFoundationServer tfs)
            where T : class
        {
            return Translate(tfs) as T;
        }

        /// <summary>
        /// Creates a new instance of the <see cref="TfsEventArgs"/>
        /// class.
        /// </summary>
        /// <param name="eventData">The event data.</param>
        /// <param name="identityData">The identity data.</param>
        /// <param name="subscriptionId">The subscription id.</param>
        /// <param name="event">The event.</param>
        /// <param name="identity">The identity.</param>
        public TfsKnownEventArgs(XElement eventData, XElement identityData, object @event, TFSIdentity identity, int subscriptionId)
        {
            EventData = eventData;
            IdentityData = identityData;
            SubscriptionId = subscriptionId;
            Event = (T)@event;
            Identity = identity;
        }

        private static int NoFailParse(string message)
        {
            int result;
            if (!int.TryParse(message, out result))
                return -1;
            else
                return result;
        }
    }
}
